package com.runen.infinitemovement.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.chad.library.adapter.base.listener.OnLoadMoreListener;
import com.runen.infinitemovement.Constant;
import com.runen.infinitemovement.R;
import com.runen.infinitemovement.adapter.SportsRecordAdapter;
import com.runen.infinitemovement.base.BaseActivity;
import com.runen.infinitemovement.model.api.ChallengeAPI;
import com.runen.infinitemovement.model.entity.SportsRecordEntity;
import com.runen.infinitemovement.model.entity.UserExchangeStepEntity;
import com.runen.infinitemovement.widget.CustomLoadMoreView;
import com.runen.rxnetlibrary.net.callback.BaseResultCallback;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SportsRecordActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener {

    @BindView(R.id.top_btn_left)
    Button topBtnLeft;
    @BindView(R.id.top_title)
    TextView topTitle;
    @BindView(R.id.rv_sports_record)
    RecyclerView rvSportsRecord;
    @BindView(R.id.srl)
    SwipeRefreshLayout srl;
    @BindView(R.id.tv_step_num)
    TextView tv_step_num;
    @BindView(R.id.tv_mileage)
    TextView tv_mileage;
    private int type;
    private int page = 2;
    private SportsRecordAdapter recordAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_sports_record;
    }

    @Override
    protected void initView(@Nullable Bundle savedInstanceState) {
        ButterKnife.bind(this);
        srl.setOnRefreshListener(this);
        type = getIntent().getIntExtra("type", 0);
        switch (type) {
            case 0:
                topTitle.setText("跑步记录");
                break;
            case 1:
                topTitle.setText("骑行记录");
                break;
            case 2:
                topTitle.setText("健走记录");
                break;
        }
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        rvSportsRecord.setLayoutManager(layoutManager);
        recordAdapter = new SportsRecordAdapter(this);
        recordAdapter.getLoadMoreModule().setLoadMoreView(new CustomLoadMoreView());
        rvSportsRecord.setAdapter(recordAdapter);


        initData();

        recordAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Intent intent = new Intent(SportsRecordActivity.this, WebActivity.class);
                intent.putExtra("LINK_URL", String.format(Constant.SPORTS_DETAILS, recordAdapter.getData().get(position).sportRecordId));
                startActivity(intent);
            }
        });
        initLoadMore();
    }

    private void initLoadMore() {
        recordAdapter.getLoadMoreModule().setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
                loadMoreRequested();
            }
        });
        recordAdapter.getLoadMoreModule().setAutoLoadMore(true);
        recordAdapter.getLoadMoreModule().setEnableLoadMoreIfNotFullPage(false);
    }

    private void initData() {
        initUserExchangeStep();
        String str = "?limit=20&page=1&type=%s";
        srl.setRefreshing(true);
        new ChallengeAPI().sportsRecord(String.format(str, type), new BaseResultCallback<SportsRecordEntity>() {
            @Override
            public void onResponse(SportsRecordEntity response) {
                srl.setRefreshing(false);
                if (response.code == 0) {
                    if (response.data != null && response.data.size() > 0) {
                        recordAdapter.setNewData(response.data);
                        if (recordAdapter.getData().size() >= response.count) {
                            recordAdapter.getLoadMoreModule().loadMoreEnd(true);
                            recordAdapter.getLoadMoreModule().setEnableLoadMore(false);
                        }

                    }
                }
            }
        });
    }

    private void initUserExchangeStep() {
        new ChallengeAPI().userExchangeStep(new BaseResultCallback<UserExchangeStepEntity>() {
            @Override
            public void onResponse(UserExchangeStepEntity response) {
                if (response.code == 0 && response.data != null) {
                    tv_step_num.setText(response.data.cumulativeSteps + "步");
                    tv_mileage.setText(response.data.cumulativeMileage + "km");
                }
            }
        });
    }

    @OnClick(R.id.top_btn_left)
    public void onViewClicked() {
        finish();
    }

    @Override
    public void onRefresh() {
        page = 2;
        initData();
    }

    public void loadMoreRequested() {
        String str = "?limit=20&page=%s&type=%s";
        new ChallengeAPI().sportsRecord(String.format(str, page, type), new BaseResultCallback<SportsRecordEntity>() {
            @Override
            public void onResponse(SportsRecordEntity response) {
                if (response.code == 0) {
                    if (response.data != null && response.data.size() > 0) {
                        recordAdapter.addData(response.data);
                        if (recordAdapter.getData().size() >= response.count) {
                            recordAdapter.getLoadMoreModule().loadMoreEnd(true);
                            recordAdapter.getLoadMoreModule().setEnableLoadMore(false);
                        } else {
                            page++;
                            recordAdapter.getLoadMoreModule().loadMoreComplete();
                            recordAdapter.getLoadMoreModule().setEnableLoadMore(true);
                        }
                    }
                }
            }
        });
    }
}
